package com.yijiaw.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.yijiaw.domain.Part;
import com.yijiaw.domain.Parttype;
import com.yijiaw.service.PartService;
import com.yijiaw.service.ParttypeService;
import com.yijiaw.util.FileUploadUtil;


@Controller
@RequestMapping("/admin")
public class PartController {

	@Autowired
	PartService partService;
	
	@Autowired
	ParttypeService parttypeService;
	
	@Autowired
	FileUploadUtil fileUploadUtil;
	
	/**
	 * 查看全部列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/part")
	public String accessories(Model model) {
		List<Part> list = partService.findAllParts(0, partService.countParts());
		model.addAttribute("title", "配件管理");
		model.addAttribute("list", list);
		model.addAttribute("target", "part");
		
		return "admin/tab";
	}
	
	
	/**
	 * 查看单个配件详细
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/part/view/{id}")
	public String view(@PathVariable int id, Model model) {
		Part product = partService.findPartByPrimaryKey(id);
		List<Parttype> list = parttypeService.findAllParttypes(0, parttypeService.countParttypes());
		
		model.addAttribute("product", product);
		model.addAttribute("list", list);
		model.addAttribute("target", "../update");
		
		return "admin/view";
	}
	
	/**
	 * 删除配件
	 * @param id
	 * @return
	 */
	@RequestMapping("/part/delete/{id}")
	public String delete(@PathVariable int id) {
		Part part = partService.findPartByPrimaryKey(id);
		if (part != null) {
			partService.deletePart(part);
		}
		return "redirect:../";
	}
	
	/**
	 * 显示添加表单
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/part/add", method = RequestMethod.GET)
	public String add(Model model) {
		List<Parttype> list = parttypeService.findAllParttypes(0, parttypeService.countParttypes());
		model.addAttribute("list", list);
		model.addAttribute("target", "add");
		
		return "admin/view";
	}
	
	/**
	 * 添加零件
	 * @param part
	 * @param type
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/part/add", method = RequestMethod.POST)
	public String add(@ModelAttribute Part part, @RequestParam int type, @RequestParam("file") MultipartFile file, HttpServletRequest request) {
		Parttype parttype= parttypeService.findParttypeByPrimaryKey(type);
		if (file.getSize() > 0) {
			String path = request.getSession().getServletContext().getRealPath("upload/images/");
			String image = fileUploadUtil.write(file, path);
			part.setImage(image);
		}
		part.setProducttype(parttype);
		
		partService.savePart(part);
		
		return "redirect:.";
	}
	
	
	@RequestMapping("/part/update")
	public String update(@ModelAttribute Part part, @RequestParam int type, @RequestParam("file") MultipartFile file, HttpServletRequest request) {
		if (file.getSize() > 0) {
			String path = request.getSession().getServletContext().getRealPath("upload/images/");
			String image = fileUploadUtil.write(file, path);
			part.setImage(image);
		}
		
		partService.savePart(part);
		partService.savePartParttype(part.getId(), parttypeService.findParttypeByPrimaryKey(type));
		
		return "redirect:.";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
